package com.kbt.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.kbt.activity.GoodForPayActivity;
import com.kbt.activity.R;
import com.kbt.listener.ClickConfirmOrderListener;
import com.kbt.listener.ClickToPayListener;
import com.kbt.model.data.*;
import com.kbt.model.holder.OrderInfoHolder;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.tool.RestListViewSizeTool;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhgk
 * @package: com.kbt.adapter
 * @TODO: 全部订单的适配器
 * @data: 2015/8/20 9:28
 * @version: V1.0
 */
public class OrderWaitPayAdapter extends OrderBaseAdapter {

    public OrderWaitPayAdapter(Activity context, List<OrderBean> orderList) {
        super(context, orderList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        OrderInfoHolder itemHolder;

        if (convertView == null) {
            itemHolder = new OrderInfoHolder();

            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.good_order_item, null);

            itemHolder.setOrderNoView((TextView) convertView.findViewById(R.id.orderNo));
            itemHolder.setOrderCreateTimeView((TextView) convertView.findViewById(R.id.createTime));

            itemHolder.setGoodListView((ListView) convertView.findViewById(R.id.goodListForOrder));

            itemHolder.setOrderStatus((TextView) convertView.findViewById(R.id.orderStatus));

            itemHolder.setOrderCount((TextView) convertView.findViewById(R.id.orderCount));
            itemHolder.setOrderFee((TextView) convertView.findViewById(R.id.totalFeel));

            itemHolder.setBtnA((Button) convertView.findViewById(R.id.orderBtn_A));
            itemHolder.setBtnB((Button) convertView.findViewById(R.id.orderBtn_B));


            convertView.setTag(itemHolder);
        } else {
            itemHolder = (OrderInfoHolder) convertView.getTag();
        }

        final OrderBean orderInfo = orderList.get(position);
        itemHolder.getOrderNoView().setText("订单号：" + orderInfo.getOrder_no());

        itemHolder.getOrderCreateTimeView().setText(orderInfo.getOrder_create_time());

        //订单商品列表
        List<OrderGoodsListBean> goodList = orderInfo.getGoods_list();
        GoodForOrderItemAdapter itemAdapter = new GoodForOrderItemAdapter(context, goodList);
        itemHolder.getGoodListView().setAdapter(itemAdapter);
        RestListViewSizeTool.setListViewHeightBasedOnChildren(itemHolder.getGoodListView());

        if(orderInfo.getOrder_status().equals("1")) {
            itemHolder.getOrderStatus().setText("待付款");
        }

        itemHolder.getOrderCount().setText("共计 " + orderInfo.getGoods_count() + " 件");
        itemHolder.getOrderFee().setText(String.valueOf(orderInfo.getOrder_real_je()));
//        BtnSetting.setBtnBackground(itemHolder.getBtnA(), 2, Color.RED, Color.RED, 5);
        itemHolder.getBtnA().setText("去付款");
        itemHolder.getBtnA().setTextColor(Color.WHITE);
        itemHolder.getBtnA().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ShopCarBean> list = new ArrayList<ShopCarBean>();
                ShopCarBean shopCarBean ;
                for (int i = 0; i < orderInfo.getGoods_list().size();i++) {
                    shopCarBean = new ShopCarBean();
                    shopCarBean.setGoods_new_price(Float.parseFloat(orderInfo.getGoods_list().get(i).getGoods_new_price()));
                    shopCarBean.setGoods_count(orderInfo.getGoods_list().get(i).getGoods_count());
                    shopCarBean.setGoods_title(orderInfo.getGoods_list().get(i).getGoods_title());
                    shopCarBean.setGoods_icon(orderInfo.getGoods_list().get(i).getGoods_icon());
                    shopCarBean.setGoods_id(orderInfo.getGoods_list().get(i).getGoods_id());
                    shopCarBean.setGoods_tm(orderInfo.getGoods_list().get(i).getGoods_tm());
                    shopCarBean.setGoods_no(orderInfo.getGoods_list().get(i).getGoods_no());
                    list.add(shopCarBean);
                }
                Intent intent = new Intent(context, GoodForPayActivity.class);
                intent.putExtra("biaoji", "noPayMoney");
                intent.putExtra("orderNo",orderInfo.getOrder_no());
                intent.putExtra("order_jf_sub_je",orderInfo.getOrder_jf_sub_je());
                intent.putExtra("list", (Serializable) list);
                context.startActivity(intent);
//                context.finish();
            }
        });

//        BtnSetting.setBtnBackground(itemHolder.getBtnB(), 2, Color.rgb(205, 205, 205), Color.rgb(205, 205, 205), 5);
        itemHolder.getBtnB().setText("取消订单");
        itemHolder.getBtnB().setTextColor(Color.WHITE);
        itemHolder.getBtnB().setOnClickListener(new ClickConfirmOrderListener(context, convertView,
                OrderWaitPayAdapter.this, orderList, position, StaticData.POPWINDOW_CANCEL_ORDER));
        return super.getView(position, convertView, parent);
    }
}
